// filename child.c
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#define SHM_SIZE 1024
#define SEM_NAME "/my_semaphore"
int main() {
    key_t key = ftok("shared_memory_example", 65);
    int shmid;
    void *shmaddr;
    // 通过键值获取共享内存标识符
    if ((shmid = shmget(key, SHM_SIZE, 0666)) == -1) {
        perror("shmget");
        exit(1);
    }
    // 将共享内存附加到子进程的地址空间
    if ((shmaddr = shmat(shmid, (void *)0, 0)) == (void *)-1) {
        perror("shmat");
        exit(1);
    }
    // 向共享内存写入数据
    char *data = shmaddr;
    strcpy(data, "asdfa Hello from child!");
    sleep(1);
    // 通常在程序结束时执行
    // shmdt(shmaddr);
    printf("Child process finished writing to shared memory.\n");
    return 0;
}